package com.yunpai.thread;

/**
 * 线程通信
 */
public class CommunicationThread implements Runnable {
 int number = 1;

    @Override
    public void run() {

        while (true) {

            synchronized (this) {
                notify();
                if (number <= 100) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;

                } else {
                    break;
                }
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

            }
        }
    }

    public static void main(String[] args) {
        CommunicationThread communicationThread = new CommunicationThread();
        Thread thread1 = new Thread(communicationThread);
        Thread thread2 = new Thread(communicationThread);
        thread1.setName("线程一");
        thread2.setName("线程二");
        thread1.start();
        thread2.start();

    }
}
